$(function () {

    var insurance_datagrid = $("#insurance_datagrid");
    var insurance_dialog = $("#insurance_dialog");
    var insurance_form = $("#insrance_form");
    var carp_dialog = $("#carp_dialog");
    var carp_datagrid = $("#carp_datagrid");
    var product_table = $("#product_table");
    var methodObj = {
        save: function () {
            var rows = $("#product_table").datagrid("getRows");
            var ids = $.map(rows, function (item) {
                return item.id;
            })
            //提交表单
            insurance_form.form("submit", {
                url: '/insuranceTS/saveOrUpdate.do',
                onSubmit: function (param) {
                    param.ids = ids;
                    param.amount = $("#totalMoney").text();
                },
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', data.msg, 'info');
                        insurance_form.form("clear");
                        insurance_datagrid.datagrid("load");
                        insurance_dialog.dialog("close");
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            });
        },
        reload: function () {
            insurance_datagrid.datagrid("reload");
        },
        //取消按钮
        cancel: function () {
            insurance_dialog.dialog("close");
        },
        carp_cancel: function () {
            carp_dialog.dialog("close");
        },
        //拒保
        refuse: function () {
            //如果没有选择数据
            var row = insurance_datagrid.datagrid("getSelected");
            if (!row) {
                //弹出提示框
                $.messager.alert("提示", "请选择数据", "warning");
                return;
            }

            $.get('/insuranceTS/refuse.do', {id: row.id}, function (data) {

                if (data.success) {
                    $.messager.alert('温馨提示', data.msg, 'info', function () {
                        insurance_datagrid.datagrid("reload");
                    });
                } else {
                    $.messager.alert('温馨提示', data.msg, 'error');
                }
            }, 'json');
        },
        //添加保险产品明细
        carp_add: function () {

            //var row = carp_datagrid.datagrid("getSelected");
            var row = carp_datagrid.datagrid("getSelected");
            //var insurance_table = $("#insurance_table");
            $("#product_table").datagrid("appendRow", row);

            count();
            carp_dialog.dialog("close");

        },
        //提审
        prepareAudit: function () {
            //如果没有选择数据
            var row = insurance_datagrid.datagrid("getSelected");
            if (!row) {
                //弹出提示框
                $.messager.alert("提示", "请选择数据", "warning");
                return;
            }

            $.get('/insuranceTS/prepareAudit.do', {id: row.id}, function (data) {

                if (data.success) {
                    $.messager.alert('温馨提示', data.msg, 'info', function () {
                        insurance_datagrid.datagrid("reload");
                    });
                } else {
                    $.messager.alert('温馨提示', data.msg, 'error');
                }
            }, 'json');
        },
        //编辑
        edit: function () {

            //如果没有选择数据
            var row = insurance_datagrid.datagrid("getSelected");
            if (!row) {
                //弹出提示框
                $.messager.alert("提示", "请选择数据", "warning");
                return;
            }
            //投保人数据回显
            row["client.id"] = row.client.id
            row["client.name"] = row.client.name;
            row["client.sex"] = row.client.sex;
            row["client.tel"] = row.client.tel;
            row["client.id_card"] = row.client.id_card;
            row["client.address"] = row.client.address;

            //车辆信息回显
            row["carInformation.id"] = row.carInformation.id;
            row["carInformation.name"] = row.carInformation.name;
            row["carInformation.brand"] = row.carInformation.brand;
            row["carInformation.type"] = row.carInformation.type;
            row["carInformation.carNumber"] = row.carInformation.carNumber;
            row["carInformation.airVolume"] = row.carInformation.airVolume;
            row["carInformation.state"] = row.carInformation.state;
            row["carInformation.inputTime"] = row.carInformation.inputTime;
            row["carInformation.evaluation"] = row.carInformation.evaluation;
            row["carInformation.carType"] = row.carInformation.carType;
            row["productItem.id"] = row.productItem.id;
            row["productItem.name"] = row.productItem.name;

            $("#mech_combo").combobox("setValue", row.productItem.id);

            $.get("/insuranceTS/getCarproductByInsuId.do", {InsuId: row.id}, function (data) {
                product_table.datagrid({
                    data: data,
                    fit: true,
                    fitColumns: true,
                    striped: true,
                    singleSelect: true,
                    columns: [[
                        {field: 'id', title: 'id', width: 100, hidden: true},
                        {field: 'sn', title: '保险编号', width: 100},
                        {field: 'name', title: '保险名称', width: 100},
                        {
                            field: 'mech', title: '保险机构', width: 100, formatter: function (value, row, index) {

                                return row.mech != null ? row.mech.name : ""
                            }
                        },
                        {field: 'insured', title: '保额', width: 100},
                        {field: 'moneyYear', title: '基本年费', width: 100},
                        {
                            field: 'state', title: '销售状态', width: 100, formatter: function (value, row, index) {
                                return value ? "<font color='green'>在售</font>" : "<font color='red'>停售</font>"
                            }
                        },
                        {
                            field: 'deductState', title: '不计免赔', width: 100, formatter: function (value, row, index) {
                                return value ? "<font color='green'>是</font>" : "<font color='red'>否</font>"
                            }
                        },
                    ]],

                })
                count();

            })
            insurance_form.form('load', row);

            insurance_dialog.dialog("setTitle", "编辑保单");
            insurance_dialog.dialog("open");

        },

        addProduct: function () {

            var textbox = $("#mech_combo").combobox("getValue");
            var data_grid = null;
            $.get("/carproduct/getCarproductByMechId.do", {mechId: textbox}, function (data) {
                carp_datagrid.datagrid({
                    data: data,
                    fit: true,
                    fitColumns: true,
                    striped: true,
                    singleSelect: true,
                    columns: [[
                        {field: 'sn', title: '保险编号', width: 100},
                        {field: 'name', title: '保险名称', width: 100},
                        {
                            field: 'mech', title: '保险机构', width: 100, formatter: function (value, row, index) {

                                return row.mech != null ? row.mech.name : ""
                            }
                        },
                        {field: 'insured', title: '保额', width: 100},
                        {field: 'moneyYear', title: '基本年费', width: 100},
                        {
                            field: 'state', title: '销售状态', width: 100, formatter: function (value, row, index) {
                                return value ? "<font color='green'>在售</font>" : "<font color='red'>停售</font>"
                            }
                        },
                        {
                            field: 'deductState', title: '不计免赔', width: 100, formatter: function (value, row, index) {
                                return value ? "<font color='green'>是</font>" : "<font color='red'>否</font>"
                            }
                        },
                    ]],

                })

            })

            carp_dialog.dialog("setTitle", "添加产品");
            carp_dialog.dialog("open");
        },
        //高级查询
        query: function () {
            insurance_datagrid.datagrid('load', {
                keyword: $("#keyword").val(),
                sn: $("#sn").val(),
            });
        }

    }


    //事件统一绑定
    $("[data-cmd]").click(function () {

        var cmd = $(this).data("cmd");
        methodObj[cmd]();

    });


    insurance_datagrid.datagrid({

        fit: true,
        fitColumns: true,
        rownumbers: true,
        pagination: true,
        singleSelect: true,
        toolbar: '#insurance_toolbar',
        url: '/insuranceTS/queryCorrect.do',
        columns: [[
            {field: 'sn', title: '保单编号', width: 100},
            {
                field: 'client', title: '投保人', width: 100,
                formatter: function (value, row, index) {
                    return value.name ? value.name : "";
                }
            },
            {field: 'inputtime', title: '申请日期', width: 100},
            {field: 'verifytime', title: '核保日期', width: 100},
            {field: 'term', title: '保险时长(/年)', width: 100},
            {
                field: 'saleMan', title: '业务员', width: 100,
                formatter: function (value, row, index) {
                    return row.client.takeUser != null ? row.client.takeUser.realname : "没有人"
                }
            },
            {field: 'amount', title: '投保总金额', width: 100},
            {
                field: 'state', title: '状态', width: 100,
                formatter: function (value, row, index) {
                    if (value == 0) {
                        return "暂存"
                    } else if (value == 1) {
                        return "待审核"
                    } else if (value == -1) {
                        return "拒保"
                    } else if (value == -2) {
                        return "批改"
                    }
                }
            },
        ]]

    });

    //初始化弹出框
    $("#insurance_dialog").dialog({
        width: 800,
        height: 600,
        buttons: '#buttons',
        constrain: true,
        closed: true
    });

    carp_dialog.dialog({
        width: 700,
        height: 450,
        closed: true,
        buttons: '#carp_buttons',
    })

    $("#product_table").datagrid({
        fit: true,
        fitColumns: true,
        striped: true,
        singleSelect: true,
        toolbar: "#product_table_button",
        columns: [[
            {field: 'id', title: 'id', width: 100, hidden: true},
            {field: 'sn', title: '保险编号', width: 100},
            {field: 'name', title: '保险名称', width: 100},
            {
                field: 'mech', title: '保险机构', width: 100, formatter: function (value, row, index) {

                    return row.mech != null ? row.mech.name : ""
                }
            },
            {field: 'insured', title: '保额', width: 100},
            {field: 'moneyYear', title: '基本年费', width: 100},
            {
                field: 'deductState', title: '不计免赔', width: 100, formatter: function (value, row, index) {
                    return value ? "<font color='green'>是</font>" : "<font color='red'>否</font>"
                }
            },
        ]],
    });

    $("#term").textbox({
        label: '保险时长',
        labelWidth: '70px',
        labelAlign: 'center',
        width: 140,
        onChange: function (newValue, oldValue) {
            var totalMoney = 0;
            var rows = $("#product_table").datagrid('getRows');
            for (var i = 0; i <= rows.length - 1; i++) {
                var moneyYear = rows[i].moneyYear;
                totalMoney = totalMoney + newValue * moneyYear;
            }
            $("#totalMoney").text(totalMoney);
        }
    })

})

//删除
function remove() {
    var row = $("#product_table").datagrid('getSelected');
    var index;
    if (!row) {
        $.messager.alert('温馨提示', '请选择一条数据！', 'info');
        return;
    }
    var index = $("#product_table").datagrid('getRowIndex', row);
    $("#product_table").datagrid('deleteRow', index);
    $("#product_table").datagrid('reload');

    count();
}

function count() {

    var value = $("#term").textbox("getValue");
    var totalMoney = 0;
    var rows = $("#product_table").datagrid('getRows');

    for (var i = 0; i <= rows.length - 1; i++) {
        var moneyYear = rows[i].moneyYear;
        totalMoney = totalMoney + value * moneyYear;
    }
    $("#totalMoney").text(totalMoney);

}